<template>
  <div class="login">
    <div class="login-backgound"></div>

    <div class="logo-tip">
      <img src="../assets/images/logo.png" class="logo-tip-img">
      <!-- <div class="text-box">
          <span class="logo-tip-text">亚大工程管网系统</span>
          <span class="logo-tip-text logo-tip-english">Yada Engineering Pipeline Network System</span>
        </div> -->
    </div>



    <div class="container-box">
      <img src="../assets/images/login_left.png" class="img-box">

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">物 资 管 理 系 统</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" class="passWordForm">
          <el-input v-model="loginForm.password" :type="passwordText" auto-complete="off" placeholder="密码"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
          <img v-if="!showPassword" class="passwordIcon" src="../assets/images/bkj.png" style="margin-top:7px"
              @click="changeye" />
            <i v-else class="el-icon-view passwordIcon" style="margin-top:7px" @click="changeye"></i>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
            @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <div class="footer-container">
        <div class="x-c">
          <img src="../assets/images/logo1.png" style="width: 20px; margin-right: 6px;">
          <span>上海澳马车辆物资采购有限公司</span>
        </div>
        <a href="https://beian.miit.gov.cn/" class="x-c">沪ICP备11048886号-1</a>
        <div style = "display: flex;">
          <a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=31011002001423" style = "color: black;" class="x-c">沪公网安备 31022002001423号</a>
          <span style = "margin-left: 20px;">Copyright © 2008-2024 Chinaust All Rights Reserved.</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        // username: "admin",
        // password: "admin123",
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      showPassword: false,
      passwordText: 'password'
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    changeye() {
      this.showPassword = !this.showPassword;
      if (this.showPassword === true) {
        this.passwordText = 'text';
      } else {
        this.passwordText = 'password';
      }
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.el-icon-view {
  font-size: 24px;
}

.passWordForm {
  position: relative;

  .passwordIcon {
    position: absolute;
    right: 16px;
  }

  .el-icon-view {
    color: #ACACAD;
  }

}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-backgound {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  background-image: linear-gradient(135deg, #03a2ea, #7f81ff);
  // background-color: #fff;
}

.logo-tip {
  position: fixed;
  top: 30px;
  left: 5%;
  z-index: 2;
  color: #fff;
  display: flex;
  align-items: center;

  .logo-tip-img {
    width: 112px;
  }

  .text-box {
    margin-left: 10px;
    height: 45px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .logo-tip-text {
      font-size: 22px;
      line-height: 23px;
    }

    .logo-tip-english {
      font-size: 13px;
      line-height: 13px;
    }
  }
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.container-box {
  width: 80%;
  max-width: 1200px;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .img-box {
    width: 58.33%;
    object-fit: contain;
  }
}

.login-form {
  flex-shrink: 0;
  border-radius: 6px;
  width: 400px;
  background-color: #fff;
  padding: 25px 25px 5px 25px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, .399);

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  // line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #000;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 100;
  background-color: #fff;
  border-top: 1px solid #e0e0e0;

  .footer-container {
    height: 40px;

    margin: 0 auto;
    max-width: 1200px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .x-c {
      display: flex;
      align-items: center;
    }

    span {
      display: inline-block;
    }

    a {
      color: #409EFF;
    }
  }
}

.login-code-img {
  height: 38px;
}

@media screen and (max-width: 1200px) {
  .container-box {
    width: auto;
  }

  .img-box {
    display: none;
  }

  .logo-tip {
    display: none;
  }

  .footer-container {
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    .x-c {
      display: none;
    }

    // justify-content: center !important;
  }

}
</style>
